package com.tac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tac.common.Exception.CosTomException;
import com.tac.common.result.Result;
import com.tac.dao.UserDao;
import com.tac.pojo.User;
import com.tac.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 注册
     * @param user
     * @return
     */
    public Result register(User user,Integer verification){
        //判断手机号是否已存在
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone,user.getPhone());
        User one = this.getOne(queryWrapper);
        if(one!=null){
            throw new CosTomException("手机号已注册请登录或者找回密码");
        }
        Boolean aBoolean = redisTemplate.hasKey(user.getPhone()+"");
        if(!aBoolean){
            throw new CosTomException("验证码错误或过期");
        }
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Integer code = (Integer) valueOperations.get(user.getPhone()+"");
        if(code==null| !code.equals(verification)){
            throw new CosTomException("验证码错误或过期");
        }
        boolean save = this.save(user);
        if(save){
            return Result.success(null,"success",201);
        }
        throw new CosTomException("注册失败");
    }

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    public Result<Integer> verification(Long phone){
        Random random = new Random();
        Integer code = random.nextInt(9999);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set(phone+"",code,60L, TimeUnit.SECONDS);
        return Result.success(code,"success");
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    public Result updateUser(User user){
        //修改指定字段的两种方法
        LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(User::getId,user.getId());
        lambdaUpdateWrapper.set(user.getSex()!=null,User::getSex,user.getSex());
        lambdaUpdateWrapper.set(user.getHaedImage()!=null,User::getHaedImage,user.getHaedImage());
        lambdaUpdateWrapper.set(user.getBirthday()!=null,User::getBirthday,user.getBirthday());
        lambdaUpdateWrapper.set(user.getName()!=null,User::getName,user.getName());
        boolean b = this.update(null,lambdaUpdateWrapper);
//        User user1 = new User();
//        user1.setId(user.getId());
//        user1.setSex(user.getSex());
//        user1.setHaedImage(user.getHaedImage());
//        user1.setBirthday(user.getBirthday());
//        boolean b = this.updateById(user1);
        if(b){
            return Result.success(null,"success");
        }
        throw new CosTomException("修改失败");
    }

    /**
     * 登录接口
     * @param phone
     * @param password
     * @return
     */
    public Result<User> login(Long phone,String password){
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getPhone,phone);
        User one = this.getOne(lambdaQueryWrapper);
        if(one==null){
            throw new CosTomException("账户不存在请先注册");
        }
        lambdaQueryWrapper.eq(User::getPassword,password);
        User user = this.getOne(lambdaQueryWrapper);
        if(user==null){
            throw new CosTomException("密码错误");
        }
        return Result.success(user,"success");
    }
}
